#! /bin/bash
lib_path=$(cd `dirname $0`; pwd)

# create lib folder
if [ ! -d "${lib_path}/lib/" ];then
    mkdir ${lib_path}/lib
fi

# compile snappy dependencies
if [ -d "${lib_path}/snappy/" ]; then
    mkdir -p ${lib_path}/snappy/build
else
    echo "Please download snappy first"
    exit 1
fi

cd ${lib_path}/snappy/build
cmake -DBUILD_SHARED_LIBS=ON ..
make
cp ./libsnappy.so ../../lib/libsnappy.so
cp ./libsnappy.so.1 ../../lib/libsnappy.so.1
cp ./libsnappy.so.1.1.10 ../../lib/libsnappy.so.1.1.10
cp ../snappy.h ../../lib/snappy.h
cp ./snappy-stubs-public.h ../../lib/snappy-stubs-public.h

# compile bitshuffle dependencies
if [ -d "${lib_path}/bitshuffle/" ]; then
    cd ${lib_path}/bitshuffle/src
    gcc -fPIC -shared -o libbitshuffle.so bitshuffle_core.c bitshuffle.c iochain.c -I../lz4 -O3 -lm
    cp ./libbitshuffle.so  ../../lib/libbitshuffle.so
    cp bitshuffle.h ../../lib/bitshuffle.h
    cp bitshuffle_core.h ../../lib/bitshuffle_core.h
    cd ../lz4
    gcc -fPIC -shared -o liblz4.so lz4.c -O3 -lm
    cp ./liblz4.so ../../lib/liblz4.so
else
    echo "Please download bitshuffle first"
    exit 1
fi

cd ${lib_path}/
g++ -shared -o ./libnative.so ./include/native.cc -L./lib -lsnappy -lbitshuffle -l lz4 -I./lib -fPIC
mv ./libnative.so ./lib/libnative.so

# snappy4cj
echo start build snappy4cj
cjpm build -V
echo end build snappy4cj
